#!/usr/bin/perl -w
##############################################################################
#
# test_CiscoRouter_Obj.pl
# =======================
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   A child class of Node.pm.  This script demonstrate how to inherit the Node.pm
#   class.  It also demonstrates how to override the login method as well as adding
#   custom behavior such as the showIPRoute method.  It also shows a way to extend
#   the "new" constructor.
#
# Steps:
#
#   1.) Instantiate a CiscoRouter object and call it C2600
#   2.) Start a telnet session to target host
#   3.) Log the entire telnet session to a file
#   4.) Login to the target host
#   5.) Execute Cisco 2600 router's "show ip route" command
#   6.) Parse the command output and extra out the default route info
#   7.) Release C2600's resource
#
# Expected result:
#
#   Telnet to cisco router and perform the show ip route command and exit cleanly.
#
##############################################################################

package main;
$VERSION = 1.01;

# If the PERLLIB environment variable is not set like
# "export PERLLIB=<lib directory>" then uncomment the
# following two lines:
# use FindBin qw($Bin); 
# use lib "$Bin/./lib";
use Node;
use CiscoRouter;

my $C2600 = undef;

if ((defined $ENV{'PAT_ROOT'}) && (defined $ENV{'PAT_LOG'})) {
  # Instantiate a CiscoRouter object and call it C2600
  $C2600 = CiscoRouter->new("172.16.15.4", "", "password", "password", 23);

  # Start a telnet session to target host
  $C2600->openTelnet();

  # Log the entire telnet session to a file
  $C2600->log($ENV{'PAT_LOG'} . "test_CiscoRouter_Obj.log");

  # Login to the target host
  #$C2600->login(qr/Login: $/, qr/Password: $/, qr/>/, qr/#/);
  $C2600->login(undef, qr/Password: $/, qr/>/, qr/#/);

  # Execute Cisco 2600 router's "show ip route" command
  $myRoute = $C2600->showIPRoute();

  # Parse the command output and extra out the default route info
  @lines = split ( /\n/, $myRoute);
  foreach $line (@lines) {
	if ($line =~ /S\*/) {
	    $line =~ m{.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).* (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})};
	    # $1 holds the first match and $2 holds the second match
	    print "\nDefault distination network: $1";
	    print "\nDefault gateway: $2\n";
	    last;
	}
  }
  # Release C2600's resource
  $C2600->DESTROY();
}

undef $C2600;
